<?php



declare (strict_types=1);

namespace app\index\controller;

use app\mall\model\MallGoods;
use app\mall\model\MallGoodsItem;
use app\mall\model\MallOrderCart;
use app\mall\model\MallUserActionCollect;
use app\mall\model\MallUsers;
use app\mall\service\UserAction;
use think\admin\helper\QueryHelper;
use think\db\Query;

/**
 * 购物车接口
 * @class Cart
 * @package plugin\wemall\controller\api\auth
 */
class Cart  extends Base
{
    /**
     * 获取购买车数据
     * @return void
     */
    public function list()
    {
        $this-> check_login();
        $list = MallOrderCart::mQuery()->with(['specs','goods'])->where('unid', $this->uid)->order('id desc')->append(['spec'])->select();
        if(count($list) > 0){
            foreach ($list as $key => &$value) {
                $value->is_collect = MallUserActionCollect::where('unid', $this->uid)->where('gcode', $value->gcode)->count();
            }
        }   
        $this->list = $list; 
        // dump($this->list->toArray());die;
        return $this->fetch();
    }

    /**
     * 更新购买车数据
     * @return void
     */
    public function update_cart(){
        $this-> check_login();
        $data = $this->_vali([
            'unid.value'     => $this->uid,
            'cartId.require'  => 'GOODS_EMPTY',
            'quantity.require' => 'Quantity_EMPTY',
        ]);
        $cart = MallOrderCart::mk()->where(['id' => $data['cartId'],'unid'=>$data['unid']])->findOrEmpty();
        if($cart->isEmpty()) $this->error('Cart Not Found！');
        if($data['quantity'] < 1){
            $cart->delete();
            $this->success('REMOVE_SUCCESS');
        }else{
            $cart->number = $data['quantity'];
            $cart->save();
            $this->success('Save Success');
        }
        $this->error('Save Fail！');

    }

    /**
     * 删除购买车数据
     * @return void
     */
    public function delete_cart(){
        $this-> check_login();
        $data = $this->_vali([
            'unid.value'     => $this->uid,
            'cartId.require'  => 'GOODS_EMPTY',
        ]);
        $cart = MallOrderCart::mk()->where(['id' => $data['cartId'],'unid'=>$data['unid']])->findOrEmpty();
        if($cart->isEmpty()) $this->error('Cart Not Found！');
        if ($cart->delete()) {
            $this->success('REMOVE_SUCCESS');
        }
        $this->error('REMOVE_FAIL');
    }

    /**
     * 修改购买车数据
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function set()
    {
        $this-> check_login();
        $data = $this->_vali([
            'unid.value'     => $this->uid,
            'ghash.require'  => 'GOODS_EMPTY',
            'number.value' => 1,
        ]);
        // 清理数量0的记录
        $map = ['unid' => $this->uid, 'ghash' => $data['ghash']];
        if ($data['number'] < 1) {
            MallOrderCart::mk()->where($map)->delete();
            MallUsers::recount($this->uid);
            $this->success('REMOVE_SUCCESS');
        }
        // 检查商品是否存在
        $gspec = MallGoodsItem::mk()->where(['ghash' => $data['ghash']])->findOrEmpty();
        $goods = MallGoods::mk()->where(['code' => $gspec->getAttr('gcode')])->findOrEmpty();
        if ($goods->isEmpty() || $gspec->isEmpty()) $this->error('GOODS Not Found！');
        // 保存商品数据
        $data += ['gcode' => $gspec['gcode'], 'gspec' => $gspec['gspec']];
        if($cart_exit = MallOrderCart::mk()->where($map)->find()){
            $data['number'] += $cart_exit['number'];
        }
        if (($cart = MallOrderCart::mk()->where($map)->findOrEmpty())->save($data)) {
            $this->success('Save Success', $cart->id);
        } else {
            $this->error('SAve Fail！');
        }
    }
}